﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.Core.Linq;
using System.Data.SqlClient;
using MethodWorx.Core.DomainModel;
using System.Data.Linq;

namespace MethodWorx.CMS.Linq
{
    public class DomainModelLinqUnitOfWork : LinqUnitOfWork
    {
        public DomainModelLinqUnitOfWork(DataContext context, IRepositoryRegistry repositories, bool inner)
            : base(context, repositories, inner)
        {
        }
        protected override void OnCommit()
        {
            try
            {
                base.OnCommit();
            }
            catch (SqlException sqlException)
            {
                this.CheckException(sqlException);
                throw;
            }
        }

        private void CheckException(SqlException sqlException)
        {
            if (sqlException.Number == 2601)
            {
                if (sqlException.Message.Contains("dbo.User") &&
                   sqlException.Message.Contains("IX_Username"))
                {
                    //
                    //  throw new exception
                    throw new UniqueKeyViolationException("Username");
                }
            }
        }
    }
}
